package com.santoker.user.service.controller.admin;

import com.santoker.baseservice.ApiResponseBody;
import com.santoker.baseservice.redis.AuthInfo;
import com.santoker.user.service.controller.BaseController;
import com.santoker.user.service.dto.vo.auth.LoginResponseVo;
import com.santoker.user.service.dto.vo.auth.LoginVo;
import com.santoker.user.service.dto.vo.user.UserInfoModifyVo;
import com.santoker.user.service.service.biz.auth.AuthAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@Api(tags = "注册登陆相关api")
@RestController
@RequestMapping("/admin/painters/apis")
public class AuthAdminController extends BaseController {

    @Autowired
    private AuthAdminService authAdminService;

    @ApiOperation(value = "账号登录", notes = "sid不同时会重新登陆，cid用于绑定到个推")
    @PostMapping(value = {"/account_login"})
    public ApiResponseBody<LoginResponseVo> accountLogin(
            @Valid @RequestBody LoginVo body, BindingResult result
    ) {
        validParams(result);

        LoginResponseVo loginResponseVo = authAdminService.accountLogin(body);
        return ApiResponseBody.createSuccessBody(loginResponseVo);
    }

    @ApiOperation(value = "登出")
    @ApiImplicitParams({})
    @GetMapping(value = {"/logout"})
    public ApiResponseBody logout(
            HttpServletRequest request
    ) {
        AuthInfo authInfo = getAuthInfo(request);
        authAdminService.logout(authInfo);
        return ApiResponseBody.createSuccessBody();
    }

    @ApiOperation(value = "编辑用户信息", notes = "编辑用户信息")
    @ApiImplicitParams({})
    @PostMapping(value = {"/update_user_info"})
    public ApiResponseBody updateUserInfo(
            @Valid @RequestBody UserInfoModifyVo body, BindingResult result
    ) {
        validParams(result);

        authAdminService.updateUserInfo(body);
        return ApiResponseBody.createSuccessBody();
    }

}
